package com.kehutong.wxapi.service.impl;

import com.kehutong.wxapi.request.*;
import com.kehutong.wxapi.response.*;
import com.kehutong.wxapi.service.TagService;
import com.kehutong.wxapi.util.KhtClient;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;

/**
 * 标签管理服务实现类
 * @author: liukunlong
 * @date: 2021-03-08 17:05
 */
public class TagServiceImpl implements TagService {
    @Inject
    private KhtClient khtClient;

    /**
     * 创建标签
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN
     * @param tagCreateRequest 创建标签请求对象
     * @return 创建标签响应对象
     * @throws Exception 异常
     */
    public TagCreateResponse tagCreate(TagCreateRequest tagCreateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/create", http -> {
            http.addParam("access_token", tagCreateRequest.getAccessToken());
            tagCreateRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(tagCreateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), TagCreateResponse.class);
    }

    /**
     * 更新标签名称
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN
     * @param tagUpdateRequest 更新标签名称请求对象
     * @return 更新标签名称响应对象
     * @throws Exception 异常
     */
    public TagUpdateResponse tagUpdate(TagUpdateRequest tagUpdateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/update", http -> {
            http.addParam("access_token", tagUpdateRequest.getAccessToken());
            tagUpdateRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(tagUpdateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), TagUpdateResponse.class);
    }


    /**
     * 删除标签
     * 请求地址：请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&id=ID
     * @param tagDeleteRequest 删除标签请求对象
     * @return 删除标签响应对象
     * @throws Exception 异常
     */
    public TagDeleteResponse tagDelete(TagDeleteRequest tagDeleteRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/delete", http -> {
            http.addParam("access_token", tagDeleteRequest.getAccessToken());
            http.addParam("tagid", String.valueOf(tagDeleteRequest.getTagid()));
        });
        return JSON.parseObject(jsonObject.toJSONString(), TagDeleteResponse.class);
    }

    /**
     * 获取标签成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=TAGID
     * @param tagGetRequest 获取标签成员请求对象
     * @return 获取标签成员响应对象
     * @throws Exception 异常
     */
    public TagGetResponse tagGet(TagGetRequest tagGetRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/get", http -> {
            http.addParam("access_token", tagGetRequest.getAccessToken());
            http.addParam("tagid", String.valueOf(tagGetRequest.getTagid()));
        });
        return JSON.parseObject(jsonObject.toJSONString(), TagGetResponse.class);
    }


    /**
     * 增加标签成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/addtagtags?access_token=ACCESS_TOKEN
     * @param addTagUsersRequest 增加标签成员请求对象
     * @return 增加标签成员响应对象
     * @throws Exception 异常
     */
    public AddTagUsersResponse addTagUsers(AddTagUsersRequest addTagUsersRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/addtagtags", http -> {
            http.addParam("access_token", addTagUsersRequest.getAccessToken());
            addTagUsersRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(addTagUsersRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), AddTagUsersResponse.class);
    }

    /**
     * 删除标签成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/deltagtags?access_token=ACCESS_TOKEN
     * @param delTagUsersRequest 删除标签成员请求对象
     * @return 删除标签成员响应对象
     * @throws Exception 异常
     */
    public DelTagUsersResponse delTagUsers(DelTagUsersRequest delTagUsersRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/deltagtags", http -> {
            http.addParam("access_token", delTagUsersRequest.getAccessToken());
            delTagUsersRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(delTagUsersRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), DelTagUsersResponse.class);
    }

    /**
     * 获取标签列表
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=ACCESS_TOKEN&id=ID
     * @param tagListRequest 获取标签列表请求对象
     * @return 获取标签列表响应对象
     * @throws Exception 异常
     */
    public TagListResponse tagList(TagListRequest tagListRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/tag/list", http -> {
            http.addParam("access_token", tagListRequest.getAccessToken());
        });
        return JSON.parseObject(jsonObject.toJSONString(), TagListResponse.class);
    }
}
